package com.zsy.srpingframework.resourceloader.resource;

import cn.hutool.core.lang.Assert;
import com.zsy.srpingframework.resourceloader.Resource;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class UrlResoucre implements Resource {
    private final URL url;

    public UrlResoucre(URL url) {
        Assert.notNull(url, "url must not be null !");
        this.url = url;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        URLConnection connection = this.url.openConnection();
        try {
            return connection.getInputStream();
        } catch (IOException e) {
            if (connection instanceof HttpURLConnection) {
                ((HttpURLConnection) connection).disconnect();
            }
            throw e;
        }
    }
}
